package com.jf.apply_car.service.impl;

import com.jf.apply_car.mapper.ApplyMapper;
import com.jf.apply_car.service.EchartsService;
import com.jf.apply_car.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Service
@Transactional
public class EchartsServiceImpl implements EchartsService {
    @Autowired
    private ApplyMapper applyMapper;

    public ResultJson getPieData() {
        return ResultJson.success(applyMapper.getPie());
    }

    public ResultJson getBarData() {

        Calendar calendar = Calendar.getInstance();
        /*获取当前月的最后一天的数字*/
        int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.DATE, 1);
        String startTime = sdf.format(calendar.getTime());
        calendar.set(Calendar.DATE, max);
        String endTime = sdf.format(calendar.getTime());
        return ResultJson.success(applyMapper.getBar(startTime, endTime));
    }

    public ResultJson getLineData() {

        Calendar calendar = Calendar.getInstance();
        int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DATE, max);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String endTime = simpleDateFormat.format(calendar.getTime());


        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 11);
        calendar.set(Calendar.DATE, 1);

        String startTime = simpleDateFormat.format(calendar.getTime());

        return ResultJson.success(applyMapper.getLine(startTime, endTime));
    }
}
